import Vue from 'vue'

import VueRouter from 'vue-router'

import { Toast } from 'vant'

Vue.use(Toast)

Vue.use(VueRouter)

const router = new VueRouter({
  // mode: 'history',
  routes: [
    {
      name: 'default',
      path: '/',
      redirect: { name: 'index' }

    },
    {
      name: 'login',
      path: '/login',
      component: () => import('@/views/login.vue')
    },
    {
      name: 'index',
      path: '/index',
      component: () => import('@/views/index.vue')
    },
    {
      name: 'details',
      path: '/details',
      component: () => import('@/views/details.vue')
    }
  ]
})
router.beforeEach((to, from, next) => {
  window.scrollTo(0, 0)
  if (to.name === 'login') {
    next()
  } else {
    const token = localStorage.getItem('mytoken')
    if (token) {
      next()
    } else {
      next('/login')
    }
  }
})
export default router
